package com.china08.yxyapi.api.home.kecheng.xiaoben;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLesson;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLessonAuth;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonAuthRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonRepository;

/**
 * 校本课程--判断家长下的孩子是否已授权
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class KechengXiaobenPatriarchIfVipController extends YxyApiBaseController {

    @Autowired
    private YxyLessonsSchoolLessonAuthRepository yxyLessonsSchoolLessonAuthRepository;

    @Autowired
    private YxyLessonsSchoolLessonRepository yxyLessonsSchoolLessonRepository;

    @RequestMapping(value = "/api_yw/home/kecheng/xiaoben/Patriarch/vip/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel find(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        List<String> studentIdLst = reqModel.studentIdLst;
        String lessonId = reqModel.lessonId;
        String schoolId = getProfile(request, response).getSchoolId();

        RespModel respModel = new RespModel();

        YxyLessonsSchoolLesson yxyLessonsSchoolLesson = yxyLessonsSchoolLessonRepository.findById(lessonId);
        for (String string : studentIdLst) {

            // 已授权标识
            YxyLessonsSchoolLessonAuth tuAuths = yxyLessonsSchoolLessonAuthRepository
                    .findByUserIdAndLessonIdAndSchoolIdAndStatus(string, lessonId, schoolId, 1);

            if (yxyLessonsSchoolLesson != null) {
                respModel.epTimes = yxyLessonsSchoolLesson.getEpTimes();
            }
            Integer aa = yxyLessonsSchoolLesson.getVip();
            if (tuAuths != null || aa == 0) {
                respModel.vip = true;
                break;
            } else {
                respModel.vip = false;
            }

        }

        return respModel;
    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiPageReqModel {

        // 家长下的学生ID
        public List<String> studentIdLst;

        // 课程ID
        public String lessonId;
    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {

        // 图标：true已授权，false授权课程
        public Boolean vip;
        // 次数
        public Integer epTimes;

    }

}
